{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2 3 4 "
     ]
    }
   ],
   "source": [
    "list1=[1,2,3,4]\n",
    "it = iter(list1)    # 创建迭代器对象\n",
    "for x in it:\n",
    "    print (x, end=\" \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输出: \n",
      "70\n",
      "(60, 50)\n"
     ]
    }
   ],
   "source": [
    "def printinfo( arg1, *vartuple ):\n",
    "   \"打印任何传入的参数\"\n",
    "   print (\"输出: \")\n",
    "   print (arg1)\n",
    "   print (vartuple)\n",
    " \n",
    "# 调用printinfo 函数\n",
    "printinfo( 70, 60, 50 )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "{'a': 2, 'b': 3}\n"
     ]
    }
   ],
   "source": [
    "def printinfo( arg1, **vardict ):\n",
    "   print (arg1)\n",
    "   print (vardict)\n",
    "\n",
    "printinfo(1, a=2,b=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 6]\n"
     ]
    }
   ],
   "source": [
    "a=[1,2,3,4]\n",
    "a[4:] = [6]\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\t5\t9\t2\t6\t10\t3\t7\t11\t4\t8\t12\t"
     ]
    }
   ],
   "source": [
    "matrix = [\n",
    "     [1, 2, 3, 4],\n",
    "     [5, 6, 7, 8],\n",
    "     [9, 10, 11, 12],\n",
    " ]\n",
    "\n",
    "for i in range(4):\n",
    "    for row in matrix:\n",
    "        print( row[i],end='\\t')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[[row[i] for row in matrix] for i in range(4)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]\n"
     ]
    }
   ],
   "source": [
    "transposed = []\n",
    "for i in range(4):\n",
    "    transposed.append([row[i] for row in matrix])\n",
    "\n",
    "print(transposed)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]\n"
     ]
    }
   ],
   "source": [
    "transposed = []\n",
    "for i in range(4):\n",
    "    transposed_row = []\n",
    "    for row in matrix:\n",
    "        transposed_row.append(row[i])\n",
    "    transposed.append(transposed_row)\n",
    "\n",
    "print(transposed)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 tic\n",
      "1 tac\n",
      "2 toe\n"
     ]
    }
   ],
   "source": [
    "for i, v in enumerate(['tic', 'tac', 'toe']):\n",
    "    print(i, v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('name', 'lancelot'), ('quest', 'the holy grail'), ('favorite color', 'blue')]\n"
     ]
    }
   ],
   "source": [
    "questions = ['name', 'quest', 'favorite color']\n",
    "answers = ['lancelot', 'the holy grail', 'blue']\n",
    "print(list( zip(questions, answers) ))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name lancelot\n",
      "quest the holy grail\n",
      "favorite color blue\n"
     ]
    }
   ],
   "source": [
    "for i, v in list( zip(questions, answers) ):\n",
    "    print(i, v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1\n",
      "1 2\n",
      "2 3\n"
     ]
    }
   ],
   "source": [
    "list1 = [(0,1),(1,2),(2,3)]\n",
    "for i,v in list1:  #元组也行\n",
    "    print(i, v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 3)\n",
      "(4, 5, 6)\n"
     ]
    }
   ],
   "source": [
    "a3 = [(1, 4), (2, 5), (3, 6)]\n",
    "a1, a2 = zip(*a3 )\n",
    "print(a1)\n",
    "print(a2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['b', 'o', 'o', 'n', 'u', 'R']\n",
      "b o o n u R \n",
      "[5, 3, 4, 2, 1]\n",
      "5 3 4 2 1 "
     ]
    }
   ],
   "source": [
    "# 元组\n",
    "seqTuple = ('R', 'u', 'n', 'o', 'o', 'b')\n",
    "print(list(reversed(seqTuple)))\n",
    "for i in reversed(seqTuple):\n",
    "    print(i, end=' ')\n",
    "print()\n",
    "# 列表\n",
    "seqList = [1, 2, 4, 3, 5]\n",
    "print(list(reversed(seqList)))\n",
    "for i in reversed(seqList):\n",
    "    print(i, end=' ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 :  1\n",
      "2 :  123\n",
      "3 :  123\n"
     ]
    }
   ],
   "source": [
    "num = 1\n",
    "def fun1():\n",
    "    global num  # 需要使用 global 关键字声明\n",
    "    print(\"1 : \",num) \n",
    "    num = 123\n",
    "    print(\"2 : \",num)\n",
    "fun1()\n",
    "print(\"3 : \",num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1: 0\n",
      "2: 0\n",
      "3: 0\n"
     ]
    }
   ],
   "source": [
    "total = 0 # 这是一个全局变量\n",
    "def sum( arg1, arg2 ):\n",
    "    print ('1:', total)\n",
    "    ss =  # total在这里是局部变量.\n",
    "    print ('2:', ss)\n",
    "    return ss\n",
    "\n",
    "sum( 10, 20 )\n",
    "print ('3:', total)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 100\n",
      "2 100\n"
     ]
    }
   ],
   "source": [
    "def outer():\n",
    "    num = 10\n",
    "    def inner():\n",
    "        nonlocal num   # nonlocal关键字声明\n",
    "        num = 100\n",
    "        print('1',num)\n",
    "    inner()\n",
    "    print('2',num)\n",
    "outer()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "ename": "UnboundLocalError",
     "evalue": "local variable 'a' referenced before assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mUnboundLocalError\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-13-4212688e84b0>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      3\u001b[0m     \u001b[0ma\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0ma\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m \u001b[0mtest\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m<ipython-input-13-4212688e84b0>\u001b[0m in \u001b[0;36mtest\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0ma\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m10\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mtest\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m     \u001b[0ma\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0ma\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      4\u001b[0m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[0mtest\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mUnboundLocalError\u001b[0m: local variable 'a' referenced before assignment"
     ]
    }
   ],
   "source": [
    "a = 10\n",
    "def test():\n",
    "    a = a + 1\n",
    "    print(a)\n",
    "test()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "22: 1\n"
     ]
    }
   ],
   "source": [
    "a = 10\n",
    "def test():\n",
    "    a =0\n",
    "    a = a + 1 \n",
    "    print('22:',a)\n",
    "    \n",
    "test()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
